package com.ext.aic.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.ext.aic.R;
import com.ext.aic.adapter.TaskListAdapter;
import com.ext.aic.apis.user.TaskListRequest;
import com.ext.aic.beans.Pagination;
import com.ext.aic.beans.TaskItem;
import com.ext.aic.beans.params.TaskListParams;
import com.ext.aic.databinding.CurrentTaskLayoutBinding;
import com.ext.aic.handler.PageHandler;

import java.util.List;

public class CurrentTaskActivity extends BaseActivity {

    private CurrentTaskLayoutBinding binding;
    private TaskListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(this, R.layout.current_task_layout, null);
        binding = CurrentTaskLayoutBinding.bind(view);
        initView();
        setContentView(binding.getRoot());
        initData();
        pageHandler.refresh();
    }

    private void initView() {
        binding.topView.backIv.setOnClickListener(this::onBackClick);
        binding.topView.titleTv.setText(R.string.current_task);

        pageHandler.registerListener(binding.refreshListSrl);
    }

    private void initData() {
        binding.currentTaskRv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new TaskListAdapter();
        adapter.setItems(pageHandler.getItems());
        binding.currentTaskRv.setAdapter(adapter);
    }

    public void onBackClick(View view) {
        finish();
    }

    private PageHandler<TaskItem> pageHandler = new PageHandler<TaskItem>() {
        @Override
        protected void onRequestData(int page, int size) {
            TaskListParams params = new TaskListParams();
            params.setPage(page);
            params.setPageSize(size);
            taskListRequest.request(CurrentTaskActivity.this, CurrentTaskActivity.this, params);
        }
    };

    private TaskListRequest taskListRequest = new TaskListRequest() {
        @SuppressLint("NotifyDataSetChanged")
        @Override
        protected void onTaskSuccess(List<TaskItem> items, Pagination pagination) {
            pageHandler.loadDone(binding.refreshListSrl, pagination, items);
            adapter.notifyDataSetChanged();
        }
    };
}
